/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>>res;
        if(root==nullptr)
        {
            return res;
        }
        queue<TreeNode*>q;
        bool flag=false;//奇false，偶true
        q.push(root);
        while(!q.empty())
        {
            vector<int>tmp;
            for(int i=q.size();i>0;--i)
            {
                auto node=q.front();
                q.pop();
                tmp.push_back(node->val);
                if(node->left)q.push(node->left);
                if(node->right)q.push(node->right);
            }
            if(flag)reverse(tmp.begin(),tmp.end());
            res.push_back(tmp);
            flag=!flag;
        }
        return res;
    }
};